# encoding: utf-8
'''
@author: atuo.cn
'''
import epubbuilder.ez_epub as ez_epub
import subprocess
import os
import shutil

class EPubRender:
    def __init__(self):
        pass
    
    def render(self, textbook, destPath):
        book = ez_epub.Book()
        book.lang = textbook.lang
        book.title = textbook.bookTitle
        book.authors = textbook.authors
        book.cover = textbook.cover
        book.sections = textbook.sections
        book.make(destPath)


class MobiRender:
    def __init__(self, kindlegen,arg=""):
        self.kindlegen=kindlegen
        self.arg=arg
    
    def render(self, textbook, destPath):
        book = ez_epub.Book()
        book.lang = textbook.lang
        book.title = textbook.bookTitle
        book.authors = textbook.authors
        book.cover = textbook.cover
        book.sections = textbook.sections
        book.make(destPath, "opf")
        opf=os.path.join(destPath, "OEBPS", "content.opf")
        outdir, outfile=os.path.split(destPath)        
        outfile+=".mobi"
        exe=[self.kindlegen, opf, self.arg, "-o", outfile]
        if not self.arg: del exe[2]
        retcode=subprocess.call(exe)
        if retcode==0 or retcode==1:
            shutil.move(os.path.join(destPath,"OEBPS",outfile),
                        os.path.join(outdir,outfile))
        